import SDK from "./NIM_Web_SDK_weixin_v6.2.0";
import {
    showToast
}
from '../utils/util.js';
import NetcallController from './netcall.js'

let app = getApp()
let store = app.store

let orderCounter = 1
export default class IMController {
    constructor(headers) {
        app.globalData.nim = SDK.NIM.getInstance({
            debug: true,
            db: false,
            appKey: app.globalData.appkey,
            token: headers.token,
            account: headers.account,
            promise: true,
            transports: ["websocket"],
            syncSessionUnread: true, // 同步未读数
            onconnect: this.onConnect,
            onwillreconnect: this.onWillReconnect,
            ondisconnect: this.onDisconnect,
            onerror: this.onError,
            // 消息
            onroamingmsgs: this.onRoamingMsgs,
            onofflinemsgs: this.onOfflineMsgs,
            onmsg: this.onMsg,
        });
        // // 发送消息开始登陆
        // store.dispatch({
        //     type: "Login_StartLogin"
        // });
    }
    /** 1
     * 连接成功
     */
    onConnect() {
        console.log(orderCounter++, ' onConnect: ')
        console.error("连接成功");
        app.globalData.netcallController = new NetcallController({
            debug: true,
            nim: app.globalData.nim
        })
    }

    onWillReconnect(obj) {
        // 此时说明 `SDK` 已经断开连接, 请开发者在界面上提示用户连接已断开, 而且正在重新建立连接

        console.log("即将重连", obj);
    }

    onDisconnect(error) {
        console.log("丢失连接");
        console.log("丢失连接", error);
    }

    onError(error) {
        console.log(" onError", error);
    }
    /**
     * 漫游消息：会多次收到，每次只会收到指定人的漫游消息
      // {scene:"p2p",sessionId:"p2p-cs4",timetag:1513153729257,to:"cs4",msg:[{from:'wujie',text:'222',to:'cs4'}]}
      // {scene:"team",sessionId:"team-3944051",timetag:1513153729257,to:"3944051",msg:[{from:'wujie',text:'222',to:'cs4'}]}
     */
    onRoamingMsgs(list) {
        console.log(orderCounter++, ' 漫游消息')
        console.log(list)

    }
    /**
     * 会话更新：收到消息、发送消息、设置当前会话、重置会话未读数 触发
     * {id:'p2p-zys2',lastMsg:{},scene,to,unread,updateTime}
     * {id:'team-1389946935',lastMsg:{attach:{accounts,team},type,users},scene,to,from,type,unread,updateTime}
     */
    onUpdateSession(session) {
        console.log('onUpdateSession: ', session)

    }
    /**会话
     * [ {id:"p2p-liuxuanlin",lastMsg:{from:'wujie',text:'222',to:"liuxuanlin"}} ]
     */
    onSessions(sessions) {
        console.log('onSessions: ', sessions)

    }
    onOfflineMsgs(msg) {
        console.log(orderCounter++, ' onOfflineMsgs')
        console.log(msg)

    }
    /**
     * 收到消息
     * {cc,flow:"in",from,fromClientType:"Web",fromDeviceId,fromNick,idClient,idServer:"9680840912",isHistoryable:true,isLocal,isMuted, isOfflinable,isPushable,isRoamingable,isSyncable,isUnreadable,needPushNick,resend,scene:"p2p",sessionId:"p2p-zys2",status:"success",target:"zys2",text:"[呕吐]",time,to:"wujie",type:"text",userUpdateTime}
     */
    onMsg(msg) {
        console.log('onMsg: 收到消息', msg)
    }
}

// 初始化的时候回返回一条数据，里面还有所有的未读数，未读数初始化状态不对，后面收到新的后就正确了
// 好友被删除后，再次推送过来的消息如有此人消息会报错，原因recentChat页是获取数据时是从好友列表中拿的